package com.ecar.apm.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ecar.apm.model.HttpSequence;
import com.ecar.apm.model.HttpSystem;
import com.ecar.apm.model.RecentStatus;

@Mapper
public interface HttpSequenceMapper {

	  @Insert({"INSERT INTO http_sequence(guid,`group`,`type`,`name`,remark,jobName,frequency)  VALUES(#{httpSequence.guid},#{httpSequence.group},#{httpSequence.type},#{httpSequence.name},#{httpSequence.remark},#{httpSequence.jobName},#{httpSequence.frequency})"})
	  public abstract void insert(@Param("httpSequence") HttpSequence paramHttpSequence);
	  
	  @Insert({"INSERT INTO http_system(`name`)  VALUES(#{httpSystem.name})"})
	  public abstract void insertSystem(@Param("httpSystem") HttpSystem paramHttpSystem);
	  
	  @Update({"update http_sequence t set t.`group`=#{httpSequence.group},t.`type`=#{httpSequence.type},t.`name`=#{httpSequence.name},t.remark=#{httpSequence.remark},t.frequency=#{httpSequence.frequency} WHERE t.`guid`= #{httpSequence.guid}"})
	  public abstract void update(@Param("httpSequence") HttpSequence paramHttpSequence);
	  
	  @Update({"UPDATE http_sequence t SET t.`enabled` = #{httpSequence.enabled} WHERE t.`guid`= #{httpSequence.guid} "})
	  public abstract void updateEnabled(@Param("httpSequence") HttpSequence paramHttpSequence);
	  
	  @Update({"UPDATE http_sequence t SET t.`archived` = 1 WHERE t.`guid`= #{guid} "})
	  public abstract void archived(@Param("guid") String paramString);
	  
	  @Select({"SELECT * FROM http_sequence"})
	  public abstract List<HttpSequence> selectAll();
	  
	  @Select({"select t.* from http_sequence t where t.guid=#{guid}"})
	  public abstract HttpSequence getByGuid(@Param("guid") String paramString);
	  
	  @Select({"select t.guid,t.`group`,t.type,t.`name`,t.frequency,t.enabled,b.`status`,c.url from http_sequence t left join ( select * from http_sequence_log where id in ( select max(id) from http_sequence_log p where p.createTime>DATE_SUB(now(),INTERVAL 1 DAY) group by pguid) ) b on t.guid=b.pguid left join http_request c on c.pguid=t.guid where t.archived = 0 order by 6 desc,7"})
	  public abstract List<Map<String, Object>> selectMonitorList();
	  
	  @Select({"select aa.guid,aa.`group`,aa.`type`,aa.name,aa.frequency,aa.enabled,aa.url,bb.avgCostTime,bb.minCost,bb.maxCost,bb.moniCount,bb.errorCount,bb.createTime,bb.lastCost,bb.`status` from ( select t.guid,t.`group`,t.`type`,t.name,t.frequency,t.enabled,c.url from http_sequence t left join http_request c on t.guid=c.pguid where t.archived = 0 ) aa left join ( select a.pguid,a.avgCostTime,a.minCost,a.maxCost,a.moniCount,case when b.errorCount is null then 0 else b.errorCount end errorCount,a.createTime,c.costTime lastCost,case when c.`status`  is null then 3 else c.`status` end `status` from ( select p.pguid,ROUND(avg(p.costTime)) avgCostTime,min(p.costTime) minCost,max(p.costTime) maxCost,count(1) moniCount,max(p.createTime) maxCreateTime,REPLACE(timediff(now(),max(p.createTime)),':','-') createTime from http_sequence_log p where p.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) group by p.pguid ) a left join (select p.pguid,count(1) errorCount from http_sequence_log p where p.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) and p.`status`=0 group by p.pguid ) b on a.pguid=b.pguid left join ( select * from http_sequence_log p where p.createTime>DATE_SUB(now(),INTERVAL 24 HOUR)  ) c on a.pguid=c.pguid and a.maxCreateTime=c.createTime ) bb on aa.guid=bb.pguid order by 6 desc,15 ,14 desc,10 desc"})
	  public abstract List<RecentStatus> selectMonitorDetailList();
	  
	  @Select({"select * from http_system"})
	  public abstract List<HttpSystem> selectAllSystem();
}
